//
//  CHRollViewLabel.m
//  CleanerAir_Project
//
//  Created by 付亚杰 on 16/7/12.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "CHRollViewLabel.h"

@interface CHRollViewLabel(){
    CGFloat _labelTextWidth;
}

@property (nonatomic, strong) UILabel *label;

@end

@implementation CHRollViewLabel

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



- (instancetype)initWithFrame:(CGRect)frame
                         text:(NSString *)aText
                    textColor:(UIColor *)aColor
                     textFont:(NSInteger)aFont{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.bounds = frame;
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"滚动背景.png"]];
        CGFloat width = [NSString widthForString:aText fontSize:aFont height:17];
        _labelTextWidth = width;
        
        self.label = [[UILabel alloc] initWithFrame:(CGRectMake(MAIN_WIDTH, 0, width, frame.size.height))];
//        self.label.bounds = (CGRectMake(MAIN_WIDTH, 0, width, frame.size.height));
        self.label.font = [UIFont systemFontOfSize:aFont];
//        self.label.textAlignment = NSTextAlignmentRight;
        self.label.textColor = aColor;
        self.label.text = aText;
        [self addSubview:self.label];
        
        [self roll];
        
    }
    return self;
    
}

- (void)roll{
    
//    CGRect frame = self.label.frame;
//    frame.origin.x = MAIN_WIDTH;
//    self.label.frame = frame;
//
//    [UIView animateWithDuration:20.0f animations:^{
//        
//        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
//        [UIView setAnimationDelegate:self];
//        [UIView setAnimationRepeatAutoreverses:NO];
//        
//        CGRect frame = self.label.frame;
//        frame.origin.x = (0 - _labelTextWidth);
//        self.label.frame = frame;
//        
//    } completion:^(BOOL finished) {
//        
//        [self roll];
//        
//    }];


    [UIView beginAnimations:@"testAnimation" context:NULL];
    [UIView setAnimationDuration:20.0f];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatAutoreverses:NO];
    [UIView setAnimationRepeatCount:999999];
    
    CGRect frame = self.label.frame;
    frame.origin.x = (0 - _labelTextWidth);
    self.label.frame = frame;
    [UIView commitAnimations];
}
@end
